from PyQt5.QtWidgets import (QApplication, QTextEdit, QVBoxLayout, 
                             QWidget, QLabel)
from PyQt5.QtCore import Qt
import re
import sys

class SimpleArrayPasteWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.array_data = []
    
    def initUI(self):
        self.layout = QVBoxLayout()
        
        # 粘贴区域
        self.paste_area = QTextEdit()
        self.paste_area.setPlaceholderText("请在此粘贴数组数据...")
        self.paste_area.textChanged.connect(self.auto_parse_data)
        
        # 简洁的结果显示区域
        self.result_label = QTextEdit()
        # self.result_label.setAlignment(Qt.AlignLeft)
        # self.result_label.setWordWrap(True)  # 启用自动换行
        
        # 状态标签
        self.status_label = QLabel("状态: 等待数据粘贴")
        self.status_label.setAlignment(Qt.AlignCenter)
        
        self.layout.addWidget(QLabel("粘贴区域:"))
        self.layout.addWidget(self.paste_area)
        self.layout.addWidget(QLabel("解析结果:"))
        self.layout.addWidget(self.result_label)
        self.layout.addWidget(self.status_label)
        self.setLayout(self.layout)
    
    def auto_parse_data(self):
        text = self.paste_area.toPlainText().strip()
        
        if not text:
            self.status_label.setText("状态: 等待数据粘贴")
            self.result_label.setText("等待粘贴数据...")
            self.array_data = []
            return
        
        self.array_data = self.parse_array_text(text)
        
        if self.array_data:
            # 简洁显示所有数据，自动换行但不添加额外格式
            self.result_label.setText(", ".join(map(str, self.array_data)))
            self.status_label.setText(f"状态: 成功解析 {len(self.array_data)} 个数据")
        else:
            self.result_label.setText("无法解析数据，请尝试以下格式:\n"
                                    "1,2,3,4\n"
                                    "1 2 3 4\n"
                                    "[1, 2, 3, 4]\n"
                                    "或每行一个数字")
            self.status_label.setText("状态: 解析失败 - 请检查数据格式")
    
    def parse_array_text(self, text):
        # 移除方括号和圆括号
        text = re.sub(r'[\[\](){}]', '', text)
        
        # 尝试不同的分隔符
        for separator in [',', '\t', '\n', ';', ' ']:
            if separator in text:
                # 分割并清理数据
                data = [item.strip() for item in text.split(separator) if item.strip()]
                if data:
                    try:
                        # 尝试转换为数字
                        array_data = []
                        for item in data:
                            # 处理可能带有千位分隔符的数字
                            item = item.replace(',', '') if ',' in item and '.' in item else item
                            try:
                                num = float(item) if '.' in item else int(item)
                                array_data.append(num)
                            except ValueError:
                                continue
                        return array_data
                    except ValueError:
                        continue
        
        # 如果没有明确的分隔符，尝试按空格分割
        data = text.split()
        if data:
            try:
                array_data = []
                for item in data:
                    item = item.replace(',', '') if ',' in item and '.' in item else item
                    try:
                        num = float(item) if '.' in item else int(item)
                        array_data.append(num)
                    except ValueError:
                        continue
                return array_data
            except ValueError:
                pass
        
        return None

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SimpleArrayPasteWidget()
    window.setWindowTitle("数组数据粘贴解析工具(简洁版)")
    window.resize(500, 400)
    window.show()
    sys.exit(app.exec_())